﻿using System;
using System.Windows.Forms;
using org.se.fs.presenter.IView.Loan;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.View.Loan;

namespace org.se.fs.view.Loan
{
    public partial class FrmBenefitLoan : Form, IRegularLoan
    {
        #region IRegularLoan Members
        public void SetLabel(string status)
        {
            label.Text = status;
        }
        public void ButtonAddVisible(bool enable)
        {
            btnAdd.Visible = enable;
        }
        public void ButtonUpdateVisible(bool enable)
        {
            btnUpdate.Visible = enable;
        }
        public void ButtonAddEnable(bool enable)
        {
            btnAdd.Enabled = enable;
        }
        public void ButtonUpdateEnable(bool enable)
        {
            btnUpdate.Enabled = enable;
        }
        public string Code
        {
            get { return txtCode.Text; }
            set { txtCode.Text = value; }
        }
        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }
        public decimal RegularAmount
        {
            get { return nudRegAmount.Value; }
            set { nudRegAmount.Value = value; }
        }
        public decimal RegularInterest
        {
            get { return nudRegInterest.Value; }
            set { nudRegInterest.Value = value; }
        }
        public decimal ExtensionInterest
        {
            get { return nudExtInterest.Value; }
            set { nudExtInterest.Value = value; }
        }
        public decimal PrincipalAmount
        {
            get { return nudPrincipalAmount.Value; }
            set { nudPrincipalAmount.Value = value; }
        }
        public decimal ServiveFee
        {
            get { return nudServiceFee.Value; }
            set { nudServiceFee.Value = value; }
        }
        public bool ReLoan
        {
            get { return chkReloanY.Checked; }
            set
            {
                chkReloanY.Checked = value;
                chkReloanN.Checked = !value;
            }
        }
        public void FocusCode()
        {
            txtCode.Focus();
        }
        public void FocusDescription()
        {
            txtDescription.Focus();
        }
        public bool IsModified{get; set; }    
        #endregion

        private readonly RegularLoan _presenter;

        public FrmBenefitLoan()
        {
            InitializeComponent();
            _presenter = new RegularLoan(this);
            //_presenter.OnAdd += PresenterOnAdd;
            //_presenter.OnError += PresenterOnError;
            //_presenter.OnModify += PresenterOnModify;
            //_presenter.OnValidate += PresenterOnValidate;
        }

        static void PresenterOnValidate(object source, FormEvent e)
        {
            if (!e.IsSuccess)
                MessageBox.Show(e.Exception.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static void FormShow()
        {
            using (var frmForm = new FrmRegularSubMenu())
            {
                frmForm.ShowDialog();
            }
        }

        private void BtnSubmitClick(object sender, EventArgs e)
        {
            _presenter.Add();
        }

        private void BtnCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnUpdateClick(object sender, EventArgs e)
        {
            _presenter.Modify();
        }

        private void FrmFamilyBackgroundSubMenuLoad(object sender, EventArgs e)
        {
            _presenter.Initialize();
        }

        public void PresenterOnError(object source, FormEvent e)
        {
            if (!e.IsSuccess)
                MessageBox.Show(e.Exception.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void PresenterOnAdd(object source, FormEvent e)
        {
            //MessageBox.Show(e.Exception.Message, e.IsSuccess ? @"Success" : @"Error", MessageBoxButtons.OK,
            //                e.IsSuccess ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            
            //if (!e.IsSuccess) return;
            //_returnValue = (DataTable) e.Value;
            //Close();
        }



        public void PresenterOnModify(object source, FormEvent e)
        {
            //MessageBox.Show(e.Exception.Message, e.IsSuccess ? @"Success" : @"Error", MessageBoxButtons.OK,
            //                e.IsSuccess ? MessageBoxIcon.Information : MessageBoxIcon.Error);

            //if (!e.IsSuccess) return;
            //_returnValue = (DataTable)e.Value;
            //Close();
        }

        private void ChkReloanYCheckedChanged(object sender, EventArgs e)
        {
            if (chkReloanY.Checked)
                chkReloanN.Checked = false;

            chkReloanY.Font = new System.Drawing.Font(chkReloanY.Font.FontFamily.Name, chkReloanY.Font.Size,
                                                      chkReloanY.Checked
                                                          ? System.Drawing.FontStyle.Bold
                                                          : System.Drawing.FontStyle.Regular,
                                                      System.Drawing.GraphicsUnit.Point, 0);
        }

        private void ChkReloanNCheckedChanged(object sender, EventArgs e)
        {
            if (chkReloanN.Checked)
                chkReloanY.Checked = false;

            chkReloanN.Font = new System.Drawing.Font(chkReloanN.Font.FontFamily.Name, chkReloanN.Font.Size,
                                                      chkReloanN.Checked
                                                          ? System.Drawing.FontStyle.Bold
                                                          : System.Drawing.FontStyle.Regular,
                                                      System.Drawing.GraphicsUnit.Point, 0);
        }

        private new void TextChanged(object sender, EventArgs e)
        {
            IsModified = true;
        }

        private void ValueChanged(object sender, EventArgs e)
        {
            IsModified = true;
        }
    }
}